<template>
  <view class="content">
    <view class="navigationBox">
      <image class="returnImg" src="../../static/img/return.png" mode="" @tap="go('../coach/index')"></image>
      <view class="title">
        教练管理
      </view>
      <image class="addImg" src="../../static/img/add.png" mode="" @tap="go('../coach/addCoach')"></image>
      <image class="searchImg" src="../../static/img/search.png" mode="" @tap="searchShow()">
      </image>
    </view>
    <view class="searchBox" v-if="searchShowStatus == true">
      <view class="pickerBox">
        <view class="iptBox pickerItem">
          <input class="ipt" type="text" placeholder="输入关键词" placeholder-class="iptPla" v-model="info.search_data">
          <view class="searchBtn" @tap="getSearch()">
            搜索
          </view>
        </view>
      </view>
    </view>
    <view class="list">
      <view class="list-box" v-for="(item,index) in list">
        <u-switch v-model="item.teacher_state" @change="change(item.teacher_id)" :activeValue="1" :inactiveValue="0"
          :size="15"></u-switch>
        <view class="list-row">
          <view class="label">
            姓名/账号
          </view>
          <view class="itemInfo">
            {{item.teacher_nickname}}
          </view>
        </view>
        <view class="list-row">
          <view class="label">
            手机号
          </view>
          <view class="itemInfo">
            {{item.teacher_phone}}
          </view>
        </view>
        <view class="list-row">
          <view class="label">
            所属店铺
          </view>
          <view class="itemInfo">
            {{item.school.school_name}}
          </view>
        </view>
        <view class="list-row">
          <view class="label">
            创建时间
          </view>
          <view class="itemInfo">
            {{item.create_time}}
          </view>
        </view>
        <view class="btn-box flex">
          <view class="check-btn" @tap="goCoachDetail(item.teacher_id)">查看</view>
          <view class="update-btn" @tap="goEditCoach(item.teacher_id)">编辑</view>
          <view class="delete-btn" @tap="deleteCoach(item.teacher_id)">删除</view>
        </view>
      </view>
    </view>

    <!-- 删除弹框 -->
    <u-modal :show="deleteShow" @confirm="deleteConfirm" ref="uModal" :asyncClose="true" :showCancelButton="true"
      @cancel="deleteCancel">
      <view style="width: 100%;text-align: center;">
        确认删除该教练信息吗？
      </view>
    </u-modal>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        info: {
          search_data: '',
          page: 1,
          size: 10
        },
        list: [],
        searchShowStatus: false,
        pageSize: 10,
        pageTotal: 0,
        hasMore: true,
        passwordShow: false,
        new_password: '',
        confirm_new_password: '',
        password_user_id: 0,
        teacher_id: '',
        deleteShow: false
      };
    },
    onLoad() {
      this.getData()
    },
    onReachBottom() {
      this.getData();
    },
    methods: {
      async change(id) {
        const {
          data: res
        } = await uni.$http.post('school/teacher/saveTeacherState', {
          teacher_id: id
        })
        if (res.code != 1) {
          return this.toast.msg(res.msg)
        }
        this.toast.msg('修改成功')
      },
      searchShow() {
        if (this.searchShowStatus == false) {
          this.searchShowStatus = true;
        } else {
          this.searchShowStatus = false;
        }
      },

      getSearch() {
        this.info.page = 1
        this.list = []
        this.getData();
      },
      confirm(e) {
        this.info.start_date = e[0].replace(/-/g, "/");
        this.info.end_date = e[e.length - 1].replace(/-/g, "/");
        this.show = false
      },
      confirmHour(e) {
        this.hourText = e.value[0].label
        this.info.type = e.value[0].id
        this.hourShow = false
      },
      async getData() {
        uni.stopPullDownRefresh();
        const {
          data: res
        } = await uni.$http.post('school/teacher/getTeacherList', this.info)
        if (res.code != 1) {
          return this.toast.msg(res.msg)
        }
        if (res.data.data.length) {
          this.list = this.list.concat(res.data.data);
          this.info.page += 1;
        } else {
          this.toast.msg('没有更多数据了！')
        }
      },
      deleteCoach(teacher_id) {
        this.delete_teacher_id = teacher_id
        this.deleteShow = true
      },
      async deleteConfirm() {
        const {
          data: res
        } = await uni.$http.post('school/teacher/delTeacher', {
          teacher_id: this.delete_teacher_id
        })
        if (res.code != 1) {
          this.deleteCancel()
          return this.toast.msg(res.msg)
        }
        this.toast.msg('删除成功')
        this.info.page = 1
        this.list = []
        this.getData();
        this.deleteCancel()
      },
      deleteCancel() {
        this.deleteShow = false
        this.delete_user_id = ''
      },

      goEditCoach(teacher_id) {
        uni.navigateTo({
          url: `/pages/coach/editCoach?teacher_id=${teacher_id}`
        })
      },
      goCoachDetail(teacher_id) {
        uni.navigateTo({
          url: `/pages/coach/coachDetail?teacher_id=${teacher_id}`
        })
      }
    }
  }
</script>

<style lang="scss">
  .content {
    .navigationBox {
      display: flex;
      align-items: center;
      height: 112rpx;
      padding: 50rpx 30rpx 0 51rpx;

      .returnImg {
        width: 52rpx;
        height: 52rpx;
      }

      .title {
        flex: 1;
        text-align: center;
      }

      .addImg {
        width: 23rpx;
        height: 23rpx;
      }

      .searchImg {
        width: 30rpx;
        height: 30rpx;
        margin-left: 30rpx;
      }
    }

    .searchBox {
      width: 670rpx;
      margin: 0 auto;
      padding: 0 20rpx;

      .pickerBox {
        font-size: 28rpx;
        height: 66rpx;
        line-height: 66rpx;
        display: flex;
        width: 100%;

        .pickerItem {
          display: flex;
          width: 100%;

          .frame {
            position: relative;
            width: 192rpx;
            text-align: center;
            color: #666666;
            border: 2rpx solid #FCF7F7;

            .lower {
              width: 42rpx;
              height: 42rpx;
              position: absolute;
              top: 50%;
              transform: translateY(-50%);
              right: 10rpx;
            }
          }
        }
      }

      .iptBox {
        display: flex;

        .ipt {
          box-sizing: border-box;
          width: 75%;
          height: 66rpx;
          padding-left: 75rpx;
          border: 2rpx solid #FCF7F7;
          border-radius: 10rpx;
        }

        .iptPla {
          height: 66rpx;
          line-height: 66rpx;
          color: #999;
          font-size: 22rpx;
        }

        .searchBtn {
          display: inline-block;
          background: #A6A6A6;
          width: 25%;
          height: 66rpx;
          line-height: 66rpx;
          text-align: center;
          border-radius: 10rpx;
          margin-left: 20rpx;
          font-size: 24rpx;
        }
      }
    }

    .list {
      width: 90%;
      margin-left: 5%;
      margin-top: 20rpx;
    }

    .list-box {
      position: relative;
      width: 100%;
      height: 378rpx;
      border-radius: 5px;
      border: 1rpx solid #E5E5E5;
      margin-bottom: 30rpx;
      padding: 20rpx 5%;

      .u-switch {
        position: absolute;
        top: 44rpx;
        right: 50rpx;
      }

      .list-row {
        display: flex;
        align-items: center;
        font-size: 28rpx;
        margin-top: 20rpx;

        .label {
          width: 150rpx;
          color: #333;
          margin-right: 30rpx;
        }

        .itemInfo {
          color: #666;
        }
      }
    }

    .btn-box {
      height: 46rpx;
      margin-top: 20rpx;
      border-top: 1rpx solid #E5E5E5;
      padding-top: 40rpx;

      view {
        width: 84rpx;
      }
    }

    .check-btn {
      margin-left: auto;
      height: 46rpx;
      background: #808080;
      font-size: 12px;
      font-weight: 400;
      color: #FFFFFF;
      text-align: center;
      line-height: 46rpx;
    }

    .update-password-btn {
      height: 46rpx;
      background: #ff8d1a;
      font-size: 12px;
      font-weight: 400;
      color: #FFFFFF;
      text-align: center;
      line-height: 46rpx;
      margin-left: 3%;
    }

    .update-btn {
      height: 46rpx;
      background: #4E88E6;
      font-size: 12px;
      font-weight: 400;
      color: #FFFFFF;
      text-align: center;
      line-height: 46rpx;
      margin-left: 3%;
    }

    .delete-btn {
      float: left;
      width: 20%;
      height: 46rpx;
      background: #D43030;
      font-size: 12px;
      font-weight: 400;
      color: #FFFFFF;
      text-align: center;
      line-height: 46rpx;
      margin-left: 3%;
    }

    .item {
      display: flex;
      align-items: center;
      padding: 0 35rpx 0 19rpx;
      width: 100%;
      height: 78rpx;
      line-height: 78rpx;
      border-radius: 10rpx;
      border: 2rpx solid #FCF7F7;
      margin-top: 20rpx;

      .label {
        display: flex;
        align-items: center;
        font-size: 28rpx;
        margin-right: 70rpx;
        width: 140rpx;
      }

      .ipt {
        flex: 1;
        position: relative;
        display: flex;
        align-items: center;
        color: #666;
      }
    }
  }
</style>